/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections;

using SdlDotNet.Graphics;

using BlindShark.GameStates;

namespace BlindShark
{
	/// <summary>
	/// Description of GameStateManager.
	/// </summary>
	public class GameStateManager
	{
		#region StaticElements
		
		private static GameStateManager instance = new GameStateManager();
		private delegate void CheckingStates(GameState state);
		private static CheckingStates checkingStates;
		private static GameState newState = null;
		
		public static void Init()
		{
			ScreenManager.Initialized();
		}
		
		public static void CleanUp()
		{
			// Nothing for now
		}
		
		public static void CheckStates()
		{
			if (newState!=null)
			{
				checkingStates(newState);
				newState = null;
			}
		}
		
		public static void ChangeState(GameState state)
		{
			newState = state;
			checkingStates = new CheckingStates(ChangingState);
		}
		
		private static void ChangingState(GameState state)
		{
			if (instance.count!=0)
			{
				instance.currentState.CleanUp();
				instance.states.Remove(instance.currentState);
			}
			state.Init();
			instance.states.Add(state);
		}
		
		public static void PushState(GameState state)
		{
			newState = state;
			checkingStates = new CheckingStates(PushingState);
		}
		
		private static void PushingState(GameState state)
		{
			state.Init();
			instance.states.Add(state);
		}
		
		public static void PopState()
		{
			if (instance.count==0)
				return;
			instance.currentState.CleanUp();
			instance.states.Remove(instance.currentState);
		}
		
		public static void HandleEvents()
		{
			instance.currentState.HandleEvents();
		}
		
		public static void Update()
		{
			instance.currentState.Update();
			Video.WindowCaption = "BlindShark - " + ClockManager.FPS + " fps";
		}
		
		public static void Draw()
		{
			for (int i=0; i<instance.count; i++)
				((GameState)instance.states[i]).Draw();
			ScreenManager.MainScreen.Update();
		}
		
		public static bool Running
		{
			get {return instance.bRunning;}
		}
		
		public static void Quit()
		{
			instance.bRunning = false;
		}
		#endregion
		
		#region DynamicElements
		private bool bRunning;
		private ArrayList states;
		
		private GameStateManager()
		{
			bRunning = true;
			states = new ArrayList();
		}
		
		private int count
		{
			get {return states.Count;}
		}
		
		private GameState currentState
		{
			get {return (GameState) states[states.Count-1];}
		}
		#endregion
	}
}
